const mongoose = require('mongoose')
const Joi = require('joi');
const carSchema = new mongoose.Schema({
    car_number: {
        type: Number,
        required: [true, "未输入车辆编号"],
        length: [8, "车辆编号格式不正确"],
    },
    car_name: {
        type: String,
        required: [true, "未输入车主姓名"],
        minlength: [2, "车主姓名长度不足"],
        maxlength: [8, "车主姓名长度过长"],
    },
    car_start: {
        type: Date,
        default: Date.now
    },
    car_end: {
        type: Date,
        default: null
    },
    park_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Park",
        required: [true, "停车场区域编号不正确"]
    }
});

const JoiSchema = Joi.object({
    car_number: Joi.string().length(8).required().error(new Error("车辆编号格式不正确")),
    car_name: Joi.string().min(2).max(8).required().error(new Error("车主姓名格式不正确")),
    car_start: Joi.date().required(),
    car_end: Joi.date().required(),
});

const Car = mongoose.model("Car", carSchema);
// const test = new Car({
//     car_number: "00000001",
//     car_name: "测试用户1",
//     park_id: '61604661f1d92fd44741a78a',
// });
// test.save();

module.exports = Car